package com.txunda.zhuohired.aty.user.my;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ants.theantsgo.gson.GsonUtil;
import com.ants.theantsgo.tools.AlertDialog;
import com.ants.theantsgo.util.JSONUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.adapter.user.my.MyBankAdapter;
import com.txunda.zhuohired.base.BaseAty;
import com.txunda.zhuohired.bean.user.my.BankListBean;
import com.txunda.zhuohired.http.pst.InfoPst;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/8 14:15
 * 功能描述：银行卡
 * 联系方式：jiahuanchen@126.com
 */

public class MyBankAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;
    /**
     * 添加
     */
    @ViewInject(R.id.tv_layout_white_right)
    private TextView tv_layout_white_right;
    /**
     * listView
     */
    @ViewInject(R.id.lv_my_bank)
    private ListView lv_my_bank;

    @ViewInject(R.id.ll_no_data)
    private LinearLayout ll_no_data;

    private MyBankAdapter myBankAdapter;
    private String token = "";
    private InfoPst infoPst;
    private List<BankListBean.DataBean> bankList;
    private String bank_type = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("银行卡");
        tv_layout_white_right.setVisibility(View.VISIBLE);
        tv_layout_white_right.setText("添加");
    }

    @Override
    @OnClick({R.id.tv_layout_white_right})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_layout_white_right://添加
                startActivity(AddBankAty.class, null);
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initHttp();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_my_bank;
    }

    @Override
    protected void initialized() {
        infoPst = new InfoPst(this);
        token = application.getUserInfo().get("data");
        bankList = new ArrayList<>();
        bank_type = getIntent().getStringExtra("bank_type");
    }

    @Override
    protected void requestData() {
        initHttp();
    }

    private void initHttp() {
        infoPst.bankList(token);
    }

    private void initAdapter() {
        myBankAdapter = new MyBankAdapter(this, bankList);
        lv_my_bank.setAdapter(myBankAdapter);
        myBankAdapter.SetOnClickListener(new MyBankAdapter.HolderClickListener() {
            @Override
            public void onSureHolderClick(int position) {
                onShowDialog(bankList.get(position).getId());
            }
        });
        if ("2".equals(bank_type)) {
            lv_my_bank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent();
                    intent.putExtra("bank_name", bankList.get(i).getBank_name());
                    intent.putExtra("bank_num", bankList.get(i).getCard_number());
                    intent.putExtra("bank_id", bankList.get(i).getId());
                    setResult(RESULT_OK, intent);
                    finish();
                }
            });
        }
    }

    private void onShowDialog(final String id) {
        new AlertDialog(this).builder().setTitle("提示").setMsg("确认解除该账号与该银行卡的关联?")
                .setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        infoPst.delBank(id);
                    }
                }).setNegativeButton("取消", new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

            }
        }).show();
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("bankList")) {
            //            L.e("==========bankList==========", jsonStr);
            BankListBean bankListBean = GsonUtil.GsonToBean(jsonStr, BankListBean.class);
            bankList = bankListBean.getData();
            if (bankList.size() == 0) {
                noData(2);
            } else {
                noData(1);
                initAdapter();
            }
            return;
        }
        if (requestUrl.contains("delBank")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            showRightTip(map.get("message"));
            initHttp();
            return;
        }
    }

    private void noData(int type) {
        if (type == 1) {
            ll_no_data.setVisibility(View.GONE);
            lv_my_bank.setVisibility(View.VISIBLE);
        } else {
            ll_no_data.setVisibility(View.VISIBLE);
            lv_my_bank.setVisibility(View.GONE);
        }
    }
}
